package com.hanbang.taitaigou.ui.activity.aboutme.myOrder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;

/**
 * 评价
 * Created by fanxin on 2016/3/23.
 */
public class AboutMyOrderEvaluateActivity extends BaseActivity {
    private ImageView quality[] = new ImageView[5];
    private ImageView speed[] = new ImageView[5];
    private ImageView attitude[] = new ImageView[5];
    private int qualityNumber = -1;
    private int speedNumber = -1;
    private int attitudeNumber = -1;

    /**商品质量**/
    private ImageView quality1,quality2,quality3,quality4,quality5;
    /**发货速度**/
    private ImageView speed1,speed2,speed3,speed4,speed5;
    /**服务态度**/
    private ImageView attitude1,attitude2,attitude3,attitude4,attitude5;
    /**提交评论**/
    private Button button;
    private String str;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_my_order_evaluate);
        initView();
        setOnClickListener();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        quality1.setOnClickListener(onClickListener);
        quality2.setOnClickListener(onClickListener);
        quality3.setOnClickListener(onClickListener);
        quality4.setOnClickListener(onClickListener);
        quality5.setOnClickListener(onClickListener);

        quality[0] = quality1;
        quality[1] = quality2;
        quality[2] = quality3;
        quality[3] = quality4;
        quality[4] = quality5;

        speed1.setOnClickListener(onClickListener);
        speed2.setOnClickListener(onClickListener);
        speed3.setOnClickListener(onClickListener);
        speed4.setOnClickListener(onClickListener);
        speed5.setOnClickListener(onClickListener);

        speed[0] = speed1;
        speed[1] = speed2;
        speed[2] = speed3;
        speed[3] = speed4;
        speed[4] = speed5;

        attitude1.setOnClickListener(onClickListener);
        attitude2.setOnClickListener(onClickListener);
        attitude3.setOnClickListener(onClickListener);
        attitude4.setOnClickListener(onClickListener);
        attitude5.setOnClickListener(onClickListener);

        attitude[0] = attitude1;
        attitude[1] = attitude2;
        attitude[2] = attitude3;
        attitude[3] = attitude4;
        attitude[4] = attitude5;

        button.setOnClickListener(onClickListener);
    }

    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_quality1:
                    qualityNumber =1;
                    setQuality(1);
                    break;
                case R.id.iv_quality2:
                    qualityNumber =2;
                    setQuality(2);
                    break;
                case R.id.iv_quality3:
                    qualityNumber =3;
                    setQuality(3);
                    break;
                case R.id.iv_quality4:
                    qualityNumber =4;
                    setQuality(4);
                    break;
                case R.id.iv_quality5:
                    qualityNumber =5;
                    setQuality(5);
                    break;
                case R.id.iv_speed1:
                    speedNumber = 1;
                    setSpeed(1);
                    break;
                case R.id.iv_speed2:
                    speedNumber = 2;
                    setSpeed(2);
                    break;
                case R.id.iv_speed3:
                    speedNumber = 3;
                    setSpeed(3);
                    break;
                case R.id.iv_speed4:
                    speedNumber = 4;
                    setSpeed(4);
                    break;
                case R.id.iv_speed5:
                    speedNumber = 5;
                    setSpeed(5);
                    break;
                case R.id.iv_attitude1:
                    attitudeNumber = 1;
                    setAttitude(1);
                    break;
                case R.id.iv_attitude2:
                    attitudeNumber = 2;
                    setAttitude(2);
                    break;
                case R.id.iv_attitude3:
                    attitudeNumber = 3;
                    setAttitude(3);
                    break;
                case R.id.iv_attitude4:
                    attitudeNumber = 4;
                    setAttitude(4);
                    break;
                case R.id.iv_attitude5:
                    attitudeNumber = 5;
                    setAttitude(5);
                    break;
                case R.id.button:
                    addComment();

                    break;
                default:
                    break;
            }
        }
    };

    //商品质量
    private void setQuality(int count) {
        // TODO Auto-generated method stub
        for (int i = 0; i < count; i++) {
            quality[i].setImageResource(R.mipmap.solid);
        }
        for (int j = count; j < 5; j++) {
            quality[j].setImageResource(R.mipmap.hollow);
        }
    }
    //发货速度
    private void setSpeed(int count) {
        // TODO Auto-generated method stub
        for (int i = 0; i < count; i++) {
            speed[i].setImageResource(R.mipmap.solid);
        }
        for (int j = count; j < 5; j++) {
            speed[j].setImageResource(R.mipmap.hollow);
        }
    }
    //服务态度
    private void setAttitude(int count) {
        // TODO Auto-generated method stub
        for (int i = 0; i < count; i++) {
            attitude[i].setImageResource(R.mipmap.solid);
        }
        for (int j = count; j < 5; j++) {
            attitude[j].setImageResource(R.mipmap.hollow);
        }
    }



    /***
     * 提交评论
     */
    private void addComment(){
        if (qualityNumber < 1) {
            Toast.makeText(this, "商品质量不能为空.", Toast.LENGTH_LONG).show();
            return;
        }
        if (speedNumber < 1) {
            Toast.makeText(this, "发货速度不能为空.", Toast.LENGTH_LONG).show();
            return;
        }
        if (attitudeNumber < 1) {
            Toast.makeText(this, "服务态度不能为空.", Toast.LENGTH_LONG).show();
            return;
        }
        HttpRequestParams httpRequestParams = new HttpRequestParams("AddComment");
        httpRequestParams.addUserId(userData);
        httpRequestParams.addBodyParameter("Order_id", str);
        httpRequestParams.addBodyParameter("quality", String.valueOf(qualityNumber));
        httpRequestParams.addBodyParameter("speed", String.valueOf(speedNumber));
        httpRequestParams.addBodyParameter("attitude", String.valueOf(attitudeNumber));
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在提交...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Intent intent = new Intent();
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        Toast.makeText(this, "提交完成", Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.setClass(AboutMyOrderEvaluateActivity.this, AboutMyOrderEvaluateOKActivity.class);
        startActivity(intent);
        finish();

    }


    /*
     * 初始化view
     */
    private void initView() {
        /**
         * 获取传递的参数
         * */
        Intent intent = getIntent();
        str = intent.getStringExtra("Order_id");

        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_evaluate), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));
        quality1 = (ImageView)findViewById(R.id.iv_quality1);
        quality2 = (ImageView)findViewById(R.id.iv_quality2);
        quality3 = (ImageView)findViewById(R.id.iv_quality3);
        quality4 = (ImageView)findViewById(R.id.iv_quality4);
        quality5 = (ImageView)findViewById(R.id.iv_quality5);

        speed1 = (ImageView)findViewById(R.id.iv_speed1);
        speed2 = (ImageView)findViewById(R.id.iv_speed2);
        speed3 = (ImageView)findViewById(R.id.iv_speed3);
        speed4 = (ImageView)findViewById(R.id.iv_speed4);
        speed5 = (ImageView)findViewById(R.id.iv_speed5);

        attitude1 = (ImageView)findViewById(R.id.iv_attitude1);
        attitude2 = (ImageView)findViewById(R.id.iv_attitude2);
        attitude3 = (ImageView)findViewById(R.id.iv_attitude3);
        attitude4 = (ImageView)findViewById(R.id.iv_attitude4);
        attitude5 = (ImageView)findViewById(R.id.iv_attitude5);

        button = (Button)findViewById(R.id.button);
    }

}
